/**
 * Created by zouyao on 2017/3/6.
 */
var myCourseCtrl = m.controller("myCoursecontroller",function ($rootScope,$scope) {

    /**
     * 初始化
     */
    $rootScope.mc_init = function () {
        $scope.prevPage = "上一页";
        $scope.nextPage = "下一页";
        $scope.currentPage = 1;
        $scope.totalPage = 1;
        $scope.constRef = [["查看详情"]];
        $scope.blurName = "";
        $scope.orderType = "";

        $scope.noScoreRecord = false;//评分记录
        $scope.myScoreRecord = 0;
        $scope.myScoreTemp = 1;

        $scope.noCommentRecord = false;//评价记录
        $scope.myCommentRecord = "";
        $scope.myCommentTemp = "";

        $scope.getCoursePageListByUserId();
    };
    /**
     * 获取课程分页列表
     */
    $scope.getCoursePageListByUserId = function(){
        /*if($scope.currentPage == 0){
            this.currentPage = 1;
        }else{
            this.currentPage = $scope.currentPage;
        }*/
        $.ajax({
            type:"POST",
            url:"/user/getCoursePageListByUserId",
            data:{"currentPage":this.currentPage,"pageSize":5,"userId":$rootScope.idOfLoger},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    /*if(data.page.list.length == 0 && $scope.currentPage > 1){
                        $scope.currentPage = $scope.currentPage - 1;
                        $scope.getCoursePageListByUserId();
                    }*/
                    $scope.courseList = new Array();
                    var obj = {};
                    for(var temp in data.value){
                        obj['id'] = data.value.id;
                        obj['username'] = data.value.username;
                        obj['email'] = data.value.email;
                        obj['tel'] = data.value.tel;
                        obj['account'] = data.value.account;
                        obj['parentId'] = data.value.parentId;
                        obj['score'] = data.value.score==null?'无':data.value.score;
                        obj['role'] = data.value.role=='student'?'学生':(data.value.role=='teacher'?'教师':(data.value.role=='leader'?'领导':'管理员'));
                        obj['courses'] = data.value.courses;
                        /*var datestr = new Date(parseInt(data.page.list[temp].createTime));
                        var temstr = datestr.getFullYear() + "年" + (parseInt(datestr.getMonth())+1) + "月" + datestr.getDate() + "日"
                            //+ datestr.getHours() + ":" + datestr.getMinutes() + ":" + datestr.getSeconds()
                            ;
                        obj['createTime'] = temstr;	//创建时间*/
                        $scope.courseList.push(obj);obj = {};
                    }
                    //分页相关更新
                    /*$scope.currentPage = data.page.current;
                    $scope.totalPage = data.page.total;*/
                });
            }
        });
    };
    /**
     * 分页操作
     * @param obj
     */
    $scope.makePagingList = function(obj){
        if(obj=="上一页"){
            if($scope.currentPage == 0){
                //nothing to do
            }else if($scope.currentPage == 1){
                alert("当前已经是第一页！");//其实并不会发生，因为disabled
            }else{
                $scope.currentPage = $scope.currentPage - 1;
                $scope.getCoursePageListByUserId();
            }
        }else if(obj=="下一页"){
            if($scope.currentPage == 0){
                //nothing to do
            }else if($scope.currentPage == $scope.totalPage){
                alert("当前已经是最后一页！");//其实并不会发生，因为disabled
            }else{
                $scope.currentPage = $scope.currentPage + 1;
                $scope.getCoursePageListByUserId();
            }
        }
    };
    /**
     * 查看教师详情模态框
     * @param obj
     */
    $scope.viewUserInfoModal = function (obj) {
        $scope.viewUserInfoTemp = obj;
        $("#modalid-viewUserInfo").modal("toggle");
    };
    /**
     * 评分模态框
     */
    $scope.scoreModal = function (obj) {
        //获取教师信息
        $.ajax({
            type:"POST",
            url:"/user/getInfoByUserId",
            data:{"userId":obj.id},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    $scope.teacherInfo = new Array();
                    var obj = {};
                    obj['id'] = data.value.id;
                    obj['username'] = data.value.username;
                    obj['email'] = data.value.email;
                    obj['tel'] = data.value.tel;
                    obj['account'] = data.value.account;
                    obj['parentId'] = data.value.parentId;
                    obj['score'] = data.value.score==null?'无':data.value.score;
                    obj['role'] = data.value.role=='student'?'学生':(data.value.role=='teacher'?'教师':(data.value.role=='leader'?'领导':'管理员'));
                    obj['courses'] = data.value.courses;
                    /*var datestr = new Date(parseInt(data.page.list[temp].createTime));
                     var temstr = datestr.getFullYear() + "年" + (parseInt(datestr.getMonth())+1) + "月" + datestr.getDate() + "日"
                     //+ datestr.getHours() + ":" + datestr.getMinutes() + ":" + datestr.getSeconds()
                     ;
                     obj['createTime'] = temstr;	//创建时间*/
                    $scope.teacherInfo.push(obj);
                });
            }
        });
        //获取我的评分记录
        $.ajax({
            type:"POST",
            url:"/score/getScoreRecordByUserId",
            data:{"studentId":$rootScope.idOfLoger,"teacherId":obj.id},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    if(data.message == "无记录" || data.value == null){
                        $scope.noScoreRecord = true;
                    }else {
                        $scope.noScoreRecord = false;
                        $scope.myScoreRecord = data.value.score;
                    }
                });
            }
        });
        $("#modalid-scoreForTeacher").modal("toggle");
    };
    /**
     * 评分
     */
    $scope.setScoreForTeacher = function () {
        $.ajax({
            type:"POST",
            url:"/score/setScoreForTeacher",
            data:{"score":$scope.myScoreTemp,"studentId":$rootScope.idOfLoger,"teacherId":$scope.teacherInfo[0].id},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    $rootScope.justForModalInfomation = "评分成功!";
                    $("#modalid-toastInfo").modal("toggle");
                    $scope.getCoursePageListByUserId();
                });
            }
        });
    };
    /**
     * 评价模态框
     */
    $scope.commentModal = function (obj) {
        //获取教师信息
        $.ajax({
            type:"POST",
            url:"/user/getInfoByUserId",
            data:{"userId":obj.id},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    $scope.teacherInfo = new Array();
                    var obj = {};
                    obj['id'] = data.value.id;
                    obj['username'] = data.value.username;
                    obj['email'] = data.value.email;
                    obj['tel'] = data.value.tel;
                    obj['account'] = data.value.account;
                    obj['parentId'] = data.value.parentId;
                    obj['score'] = data.value.score==null?'无':data.value.score;
                    obj['role'] = data.value.role=='student'?'学生':(data.value.role=='teacher'?'教师':(data.value.role=='leader'?'领导':'管理员'));
                    obj['courses'] = data.value.courses;
                    /*var datestr = new Date(parseInt(data.page.list[temp].createTime));
                     var temstr = datestr.getFullYear() + "年" + (parseInt(datestr.getMonth())+1) + "月" + datestr.getDate() + "日"
                     //+ datestr.getHours() + ":" + datestr.getMinutes() + ":" + datestr.getSeconds()
                     ;
                     obj['createTime'] = temstr;	//创建时间*/
                    $scope.teacherInfo.push(obj);
                });
            }
        });
        //获取我的评价记录
        $.ajax({
            type:"POST",
            url:"/comment/getCommentRecordByUserId",
            data:{"studentId":$rootScope.idOfLoger,"teacherId":obj.id},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    if(data.message == "无记录" || data.value == null){
                        $scope.noCommentRecord = true;
                    }else {
                        $scope.noCommentRecord = false;
                        $scope.myCommentRecord = data.value.comment;
                    }
                });
            }
        });
        $("#modalid-commentForTeacher").modal("toggle");
    };
    /**
     * 评价
     */
    $scope.setCommentForTeacher = function () {
        $.ajax({
            type:"POST",
            url:"/comment/setCommentForTeacher",
            data:{"comment":$scope.myCommentTemp,"studentId":$rootScope.idOfLoger,"teacherId":$scope.teacherInfo[0].id},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    $rootScope.justForModalInfomation = "评价成功!";
                    $("#modalid-toastInfo").modal("toggle");
                    $scope.getCoursePageListByUserId();
                });
            }
        });
    };

});